﻿using System;
using RpcClient;
using RpcHelper;
using RpcModel;
using Wedonek.RpcStore.Gateway.Interface;
using Wedonek.RpcStore.Gateway.Model;
using Wedonek.RpcStore.Service.Interface;
using Wedonek.RpcStore.Service.Model;

namespace Wedonek.RpcStore.Gateway.Service
{
        internal class ServerVisitCensusService : IServerVisitCensusService
        {
                private readonly IServerVisitCensusCollect _VisitCensus;
                private readonly IServerCollect _Server;
                public ServerVisitCensusService (IServerVisitCensusCollect visit, IServerCollect server)
                {
                        this._Server = server;
                        this._VisitCensus = visit;
                }
                public VisitCensus[] Query (IBasicPage paging, out long count)
                {
                        ServerVisitCensus[] list = this._VisitCensus.Query (paging, out count);
                        if (list.Length == 0)
                        {
                                return Array.Empty<VisitCensus>();
                        }
                        ServerConfigDatum[] servers = this._Server.Gets (list.ConvertAll (a => a.ServiceId));
                        return list.ConvertMap<ServerVisitCensus, VisitCensus> ((a, b) =>
                        {
                                ServerConfigDatum server = servers.Find (c => c.Id == a.ServiceId);
                                b.IsContainer = server.IsContainer;
                                b.ServiceState = server.ServiceState;
                                b.ServerName = server.ServerName;
                                return b;
                        });
                }

                public void Reset (long serverId)
                {
                        this._VisitCensus.Reset (serverId);
                }

                public void SetShow (Guid id, string show)
                {
                        this._VisitCensus.SetShow (id, show);
                }
        }
}
